<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState"
			class="ant-advanced-search-form mb-4">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="用户名称" name="searchKey">
						<a-select v-model:value="searchFormState.searchKey" style="width: 100%" :options="personList"
							placeholder="请选择人员" show-search :filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id', options: 'personList' }">
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="应用名称" name="appId">
						<a-select v-model:value="searchFormState.appId" style="width: 100%" :options="appList"
							placeholder="请选择应用" show-search :filter-option="filterOptionappList"
							:field-names="{ label: 'appName', value: 'appId', options: 'appList' }">
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table ref="table" :columns="columns" :data="loadData" :alert="options.alert.show"
			:row-key="(record) => record.id" :row-selection="options.rowSelection" bordered :tool-config="toolConfig">
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button danger @click="deleteBatch()">删除</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<!-- <a @click="formRef.onOpen(record)">编辑</a> -->
					<a-popconfirm title="确定要删除吗？" @confirm="deletedg(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" :personList="personList" @successful="table.refresh(true)" />
	<!-- <detail ref="detailRef" @successful="table.refresh(true)" /> -->
</template>

<script setup name="userAplay">
import { message } from 'ant-design-vue'
import Form from './form.vue'
// import detail from './detail.vue'
import sysAppruleApi from '@/api/biz/sysAppruleApi'
import appInfoApi from '@/api/biz/appInfoApi'
let searchFormState = reactive({})
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const detailRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
	{
		title: '序号',
		dataIndex: 'num'
	},
	{
		title: '应用名称',
		dataIndex: 'appName'
	},
	{
		title: '用户名称',
		dataIndex: 'userName'
	},
	{
		title: '日期',
		dataIndex: 'createTime'
	},
	{
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	}
]
let selectedRowKeys = ref([])
// 列表选择配置
const options = {
	alert: {
		show: false,
		clear: () => {
			selectedRowKeys = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const personList = ref([])
const appList = ref([])
onMounted(async () => {
	personList.value = await appInfoApi.getUsern()
	appList.value = await appInfoApi.queryAppInfoListAll()
})
const filterOption = (input, option) => {
	return option.name.indexOf(input) >= 0;
}
const filterOptionappList = (input, option) => {
	return option.appName.indexOf(input) >= 0;
}

const loadData = (parameter) => {
	const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
	return sysAppruleApi.getList(Object.assign(parameter, searchFormParam)).then((data) => {
		if (data?.records?.length > 0) {
			data.records.forEach((item, index) => {
				item.num = index + 1
			})
		}
		return data
	})
}
// 删除
const deletedg = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	sysAppruleApi.delete(params).then(() => {
		table.value.refresh(true)
	})
}
// 批量删除
const deleteBatch = () => {
	if (selectedRowKeys.value.length < 1) {
		message.warning('请选择一条或多条数据')
		return false
	}
	const params = selectedRowKeys.value.map((m) => {
		return {
			id: m
		}
	})
	sysAppruleApi.delete(params).then(() => {
		table.value.clearRefreshSelected()
	})
}
</script>
